
/* global View */

(function() {
    
    var anniversaries = {
    
        saving: false,
        collection: null,

        init: function() {
            this.bindUI();
            this.collection = new Collection();
        },
        
        bindUI: function() {
            var self = this;

            $("#dates").on('click', '.date_name', function() {
                $("#dates li").removeClass("selected");
                $(this).parents("li").addClass('selected');
            }).on('start-edit.trocar', '.date_name', function() {
                $(this).addClass('editable');
            }).on('end-edit.trocar', '.date_name', function() {
                $(this).removeClass('editable');

                if( $(this).hasClass("date_name") ) {
                    self.saveChanges('date', $.trim($(this).text()), $(this).data('id') );
                } 
            });

            $(".date_name").trocar({
                on: 'dblclick',
                off: 'blur',
                endOnEnter: true,
                filterPaste: true,
                selectOnEdit: true
            });

            $("#add_date").click(function() {
                self.addDate();
            });

            $("#dates").on('click', ".remove-link", function(e) {
                e.preventDefault();
                self.removeDate( $(this) );
            });
        },

        saveChanges: function(type, name, id, cat_id) {
            var self = this;

            if( self.saving === true ) {
                console.log("saving restricted");
                return;
            }

            self.saving = true;

            var url = $("#save-changes-url").val();
            var params = {
                action: type,
                id: id,
                name: name,
                category_id: cat_id
            };

            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: params,
                success: function(res) {
                    if( type === 'category' ) {
                        $("#categories .selected .category_name, #categories .selected .remove-link").data('id', res.OrganisationGroupType.id);
                        $("#add_group").removeAttr("disabled");
                    }

                    else if( type === 'group' ) {
                        $("#groups .selected .group_name, #groups .selected .remove-link").data('id', res.OrganisationGroup.id );
                    }

                    else if( type === 'date' ) {
                        $("#dates .selected .date_name, #dates .selected .remove-link").data('id', res.OrganisationAnniversary.id );
                    }

                    self.saving = false;
                },
                error: function(xhr) {
                    console.log(xhr.responseText);

                    self.saving = false;
                }
            });
        },

        addDate: function() {

            var dn = $("<div />").addClass("date_name pull-left").html("Enter Text Here");
            var opts = $("<div />").addClass("options pull-right").html("<a href='#' class='remove-link'><i class='fa fa-trash-o'></i></a>")
            var li = $("<li />").addClass().append(dn, ' ', opts).click(function() {
                $("#dates li").removeClass("selected");
                $(this).addClass("selected");
            });

            $("#dates").append(li);

            $(dn).trocar({
                on: 'dblclick',
                off: 'blur',
                endOnEnter: true,
                filterPaste: true,
                selectOnEdit: true
            });

            li[0].scrollIntoView(true);
            $(dn).trocar('editable', true);
        },

        removeDate: function(list_item) {
            if( !confirm("Are you sure you want to delete the selected date type?") ) {
                return;
            }

            $(list_item).parents("li").hide('slow');

            // item just added so remove immediately from DOM
            if( !$(list_item).data('id') ) {
                $(list_item).parents("li").remove();
                return;
            }

            var url = $("#groups_list").data('url');
            var params = {
                action: 'remove_date',
                date_id: $(list_item).data('id')
            };

            $.ajax({
                url: url,
                data: params,
                dataType: 'json',
                type: 'post',
                success: function(data) {
                    $(list_item).parents("li").remove();
                },
                error: function(xhr) {
                    $(list_item).parents("li").show('slow');
                    alert("A server error occured. Please try again or contact support");
                }
            });
        }
    };
    
    var GroupsView = View.extend(anniversaries);
    
    $(document).ready(function() {
        new GroupsView();
    });
})();



